/* 2016/3/27 */
package com.cosmos.pictureswitcher;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;


/**
 * A fragment representing a dialog to change picture list name.
 * <p/>
 * <p/>
 * Activities containing this fragment MUST implement the {@link com.cosmos.pictureswitcher.ListNameFragment.OnNewListNameListener}
 * interface.
 */
public class ListNameFragment extends DialogFragment {

    private static final String SHOWING_FIELD_NAME = "mShowing";

    private static final String KEY_MODE = "mode";

    private static final int MODE_CREATE = 0;

    private static final int MODE_RENAME = 1;

    private static final String KEY_ORG_NAME = "original_name";

    private String listName;

    private OnNewListNameListener onNewNameListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.fragment_list_name, null);
        final EditText listNameEditText = (EditText) dialogView.findViewById(R.id.listNameEditText);

        // Get dialog mode (create or rename), by which title is set.
        final boolean isCreate = getArguments().getInt(KEY_MODE, MODE_CREATE) == 0;
        final String oldName = getArguments().getString(KEY_ORG_NAME, null);
        if (isCreate)
            builder.setTitle(R.string.title_create_list_name);
        else {
            builder.setTitle(R.string.title_rename_list_name);
        }
        builder.setView(dialogView)
                .setPositiveButton(R.string.button_positive, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String newName = listNameEditText.getText().toString();
                        if (isCreate) {
                            onNewNameListener.onCreateList(newName);
                        } else {
                            onNewNameListener.onRenameList(oldName, newName);
                        }
                    }
                })
                .setNegativeButton(R.string.button_negative, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        return builder.create();
    }

    public void setCreateMode() {
        Bundle args = new Bundle();
        args.putInt(KEY_MODE, MODE_CREATE);
        setArguments(args);
    }

    public void setRenameMode(String oldName) {
        Bundle args = new Bundle();
        args.putInt(KEY_MODE, MODE_RENAME);
        args.putString(KEY_ORG_NAME, oldName);
        setArguments(args);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // Verify that the host activity implements the callback interface
        if (context instanceof OnNewListNameListener) {
            // Instantiate the PageNumberChangedListener so we can send events to the host
            onNewNameListener = (OnNewListNameListener) context;
        } else {
            // The activity doesn't implement the interface, throw exception
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        onNewNameListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     */
    public interface OnNewListNameListener {
        void onCreateList(String name);
        void onRenameList(String oldName, String newName);
    }
}
